/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.DispatchAction;

import dms.dto.FacultyDTO;
import dms.model.Faculty;
import dms.model.School;
import dms.model.util.PageIterator;
import dms.service.SchoolService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author leejohn
 *
 */
public class FacultyAction extends DispatchAction {
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		School school = null;
		int[] params = null;
		int start;
		int count;
		Long schoolId = null;
		PageIterator<Faculty> faculties = null;
		
		try {
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			schoolId = Long.valueOf(request.getParameter("schoolId"));
			school = schoolService.findSchoolById(schoolId);
			faculties = schoolService.findFacultyBySchool(school, start, count);
			
			request.setAttribute("faculties", faculties);
			request.setAttribute("school", school);
		} catch (Exception e) {
			log.error(e.getMessage());
			//return mapping.findForward("error");
		}
		
		return mapping.findForward("list");
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		Long schoolId = null;
		School school = null;
		
		try {
			schoolId = Long.valueOf(request.getParameter("schoolId"));
			school = schoolService.findSchoolById(schoolId);
			
			request.setAttribute("school", school);
			
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
		return mapping.findForward("addForm");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		FacultyDTO dto = (FacultyDTO) form;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		long schoolId = dto.getSchool();
		String redirectURL = "faculty.do?method=list&schoolId=" + schoolId;
		try {
			schoolService.createFaculty(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			response.sendRedirect(redirectURL);
			return null;
		}
		response.sendRedirect(redirectURL);
		
		return null;
		
	}
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long facultyId = null;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		Faculty faculty = null;
		
		try {
			facultyId = Long.valueOf(request.getParameter("id"));
			faculty = schoolService.findFacultyById(facultyId);
			
			request.setAttribute("faculty", faculty);
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
		return mapping.findForward("updateForm");
	}
	
	public ActionForward update (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		FacultyDTO dto = (FacultyDTO) form;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		long schoolId = dto.getSchool();
		String redirectURL = "faculty.do?method=list&schoolId=" + schoolId;
		try {
			schoolService.updateFaculty(dto);
			
		} catch (Exception e) {
			log.error(e.getMessage());
			response.sendRedirect(redirectURL);
			return null;
		}
		response.sendRedirect(redirectURL);
		return null;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");

		Long facultyId = null;
		Long schoolId = null;
		Faculty faculty = null;
		String redirectURL = null;

		try {			
			facultyId = Long.valueOf(request.getParameter("id"));			
			faculty = schoolService.findFacultyById(facultyId);
			schoolId = faculty.getSchool().getId();
			
			schoolService.deleteFacultyById(facultyId);		
			redirectURL = "faculty.do?method=list&schoolId=" + schoolId;
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		
		response.sendRedirect(redirectURL);
		return null;
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return this.list(mapping, form, request, response);
	}
}
